import { Controller } from "../../mvc/core/Controller";
import { CallLater } from "../../common/net/CallLater";
import { AchievementConfig } from "../resource/AchievementConfig";
import { FilterConst } from "../../component/gconst/FilterConst";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { LevelLimitDefin } from "../../common/defin/LevelLimitDefin";
import { AchievementIconBtn } from "../view/achievement/AchievementIconBtn";
import { AchievementDataProviderParser } from "../view/achievement/data/AchievementDataProviderParser";
import { IView } from "../../mvc/interfaces/IView";
import { AchievementModule } from "../../../modules/AchievementModule";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { GameProxy } from "../mvc/GameProxy";
import { EAcieveCategory } from "../../../Message/Game/EAcieveCategory";
import { DataEvent } from "../events/DataEvent";
import { AchievementMajorData } from "../view/achievement/data/AchievementMajorData";
import { AchievementMinorData } from "../view/achievement/data/AchievementMinorData";
import { AchievementFinishedHistoryWin } from "../view/achievement/win/AchievementFinishedHistoryWin";
import { LoaderHelp } from "../../common/display/LoaderHelp";
import { ResFileConst } from "../resource/ResFileConst";
import { TAchievementDetatil } from "../../../Message/Db/Tables/TAchievementDetatil";
import { TAchievementType } from "../../../Message/Db/Tables/TAchievementType";
import { MsgManager } from "../manager/MsgManager";
import { Language } from "../../../extend/language/Language";
import { AchievementFinishedWin } from "../view/achievement/win/AchievementFinishedWin";
type int = number;
//class AchievementController
    
    export  class AchievementController extends Controller
    {
        constructor()
        {
            
            super();this._finishedCodes = [];
            return;
        }

        /* internal  */achievementValueUpdateHandler(arg1: Object): void
        {
            this._module.updateAchievementValue(cache.role.money.prestige);
            return;
        }

        /* internal  */achievementCanGetBonusCodesHandler(arg1: Object): void
        {
            var evt: Object;
            var nextFrame: Function;

            var loc1;
            nextFrame = null;
            evt = arg1;
            nextFrame = (): void
            {
                if (_module != null) 
                {
                    _module.updateLatestList(cache.achievement.recentcodes);
                }
                var loc1=updateCanGetBonusEffects();
                if (loc1 >= 0 && !(_module == null)) 
                {
                    switchWinThenGotoCategory(loc1);
                }
                return;
            }
            CallLater.addCallBack(nextFrame);
            return;
        }

        /* internal  */updateCanGetBonusEffects(): int
        {
            var loc5=0;
            var loc1=AchievementConfig.instance.getCodesCategories(cache.achievement.canGetBonusCodes);
            if (loc1 == null) 
            {
                return -1;
            }
            if (this._module != null) 
            {
                this._module.updateDoingWinTabFilters(loc1, [FilterConst.achievementCanGetBonusFilter]);
            }
            var loc2=10000000;
            var loc3=0;
            while (loc3 < loc1.length) 
            {
                if ((loc5 = int(loc1[loc3])) >= 0) 
                {
                    this.icon.showAchievementEffect(true);
                    if (loc5 < loc2) 
                    {
                        loc2 = loc5;
                    }
                }
                ++loc3;
            }
            var loc4;
            if ((loc4 = int(cache.achievement.getCanGetBonusIndexs()[0])) == 0) 
            {
                this.icon.showAchievementEffect(true);
                return 0;
            }
            if (loc2 == 10000000) 
            {
                loc2 = -1;
                this.icon.showAchievementEffect(false);
                if (loc4 > 0) 
                {
                    loc2 = loc4;
                    this.icon.showAchievementEffect(true);
                }
            }
            return loc2;
        }

        /* internal  */onRoleLevelUpdateHandler(arg1: Object): void
        {
            NetDispatcher.removeCmdListener(ServerCommand.EntityInfo_Update, this.onRoleLevelUpdateHandler);
            if (cache.role.entityInfo.level < LevelLimitDefin.AchievementShowLevel) 
            {
                return;
            }
            this.icon.show();
            return;
        }

        public set isResFileGot(arg1: Boolean)
        {
            this._isResFileGot = arg1;
            return;
        }

        /* internal  */getCategoryDatas(arg1: int): Array<any>
        {
            if (arg1 == 0) 
            {
                return cache.achievement.getOverViewDatas();
            }
            return cache.achievement.getCategroyDatas(arg1);
        }

        /* internal  */checkAndShowIconEffect(): int
        {
            var loc1=int(cache.achievement.getCanGetBonusIndexs()[0]);
            if (loc1 >= 0) 
            {
                this.icon.showAchievementEffect(true);
                return loc1;
            }
            this.icon.showAchievementEffect(false);
            return -1;
        }

        /* internal  */get icon(): AchievementIconBtn
        {
            if (this._icon == null) 
            {
                this._icon = new AchievementIconBtn();
            }
            return this._icon;
        }

        public get isResFileGot(): Boolean
        {
            return this._isResFileGot;
        }

        /* internal  */get dataParser(): AchievementDataProviderParser
        {
            if (!this._dataParser) 
            {
                this._dataParser = new AchievementDataProviderParser();
            }
            return this._dataParser;
        }

        protected /* override */ initView(): IView
        {
            if (this._module == null) 
            {
                this._module = new AchievementModule();
                this.initListener();
            }
            return this._module;
        }

        /* internal  */initListener(): void
        {
            Dispatcher.addEventListener(EventName.AchievementClickTabar, this.clickTabBarHandler);
            Dispatcher.addEventListener(EventName.AchievementGetLevelUpBonus, this.getCategoryBonuseHandler);
            Dispatcher.addEventListener(EventName.AchievementGetTotalLevelUpBonus, this.getTotalLevelBonuseHandler);
            Dispatcher.addEventListener(EventName.AchievementClickShowMinorAchievemntGot, this.showMinorAchievementThatGotHandler);
            Dispatcher.addEventListener(EventName.AchievementWinChange, this.changeWinHandler);
            NetDispatcher.addCmdListener(ServerCommand.AchievementCategoryDataGot, this.categoryUpdateHandler);
            NetDispatcher.addCmdListener(ServerCommand.AchievementCategoryUpdate, this.categoryUpdateHandler);
            NetDispatcher.addCmdListener(ServerCommand.AchievementOverViewDataGot, this.overViewUpdateHandler);
            NetDispatcher.addCmdListener(ServerCommand.AchievementOverViewUpdate, this.overViewUpdateHandler);
            NetDispatcher.addCmdListener(ServerCommand.AchievementAchievementValueUpdate, this.achievementValueUpdateHandler);
            NetDispatcher.addCmdListener(ServerCommand.AchievementCanGetBonusCodes, this.achievementCanGetBonusCodesHandler);
            this._module.addEventListener(flash.events.Event.ADDED_TO_STAGE, this.onAddToStage);
            return;
        }

        protected /* override */ initServer(): void
        {
            NetDispatcher.addCmdListener(ServerCommand.AchievementFinished, this.achievementFinishedHandler);
            NetDispatcher.addCmdListener(ServerCommand.EntityInfo_Update, this.onRoleLevelUpdateHandler);
            NetDispatcher.addCmdListener(ServerCommand.RoleLevelUpdate, this.onRoleLevelUpdateHandler);
            Dispatcher.addEventListener(EventName.AchievementGetMinorBonus, this.getAchievementBonuseHandler);
            NetDispatcher.addCmdListener(ServerCommand.AchievementBonusGot, this.onBonusGotHandler);
            return;
        }

        /* internal  */onAddToStage(arg1: Object /* flash.events.Event */): void
        {
            if (this._isFirstAddToStage) 
            {
                this._isFirstAddToStage = false;
                GameProxy.achievment.getAchievementOverViewData();
                GameProxy.achievment.getCanGetBonusCodes();
                return;
            }
            var loc1=this.updateCanGetBonusEffects();
            if (loc1 >= 0) 
            {
                this.switchWinThenGotoCategory(loc1);
                return;
            }
            if (this._module.curWinIndex != AchievementModule.Index_DoingWin) 
            {
                if (this._module.curWinIndex == AchievementModule.Index_ImportantWin) 
                {
                    this.updateImportantWinWhenAddToStage();
                }
            }
            else 
            {
                this.updateDoingWinWhenAddToStage();
            }
            return;
        }

        /* internal  */updateDoingWinWhenAddToStage(): void
        {
            var categoryId: int;
            var onSyncFinished: Function;

            var loc1;
            categoryId = 0;
            onSyncFinished = null;
            onSyncFinished = (): void
            {
                _module.isCurWinLocked = false;
                updateCategory(categoryId);
                return;
            }
            categoryId = this._module.getCurTabIndex();
            if (categoryId >= 0) 
            {
                this._module.isCurWinLocked = true;
                this.syncCategoryDatas(categoryId, onSyncFinished);
            }
            return;
        }

        /* internal  */switchWinThenGotoCategory(arg1: int): void
        {
            var categoryIndex: int;
            var onSync1: Function;
            var onSync2: Function;

            var loc1;
            categoryIndex = arg1;
            if (categoryIndex < EAcieveCategory._EAchieveCategoryImportant) 
            {
                onSync1 = (): void
                {
                    _module.isCurWinLocked = false;
                    gotoCategory(categoryIndex);
                    return;
                }
                this._module.isCurWinLocked = true;
                this._module.setSelectedWin(AchievementModule.Index_DoingWin);
                this.syncCategoryDatas(categoryIndex, onSync1);
            }
            else if (categoryIndex == EAcieveCategory._EAchieveCategoryImportant) 
            {
                onSync2 = (): void
                {
                    _module.isCurWinLocked = false;
                    updateCategory(categoryIndex);
                    return;
                }
                this._module.isCurWinLocked = true;
                this._module.setSelectedWin(AchievementModule.Index_ImportantWin);
                this.syncCategoryDatas(categoryIndex, onSync2);
            }
            return;
        }

        /* internal  */updateImportantWinWhenAddToStage(): void
        {
            var category: int;
            var onDataGot: Function;

            var loc1;
            category = 0;
            onDataGot = null;
            onDataGot = (): void
            {
                _module.isCurWinLocked = false;
                updateCategory(category);
                return;
            }
            this._module.isCurWinLocked = true;
            category = EAcieveCategory._EAchieveCategoryImportant;
            this.syncCategoryDatas(category, onDataGot);
            return;
        }

        /* internal  */clickTabBarHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data["index"];
            this.syncCategoryDatas(loc1);
            return;
        }

        /* internal  */syncCategoryDatas(arg1: int, arg2: Function=null): void
        {
            if (arg1 < 0) 
            {
                if (arg2 != null) 
                {
                    arg2.apply();
                }
                return;
            }
            if (arg1 != 0) 
            {
                GameProxy.achievment.getAchiementCategoryData(arg1, arg2);
            }
            else 
            {
                GameProxy.achievment.getAchievementOverViewData(arg2);
            }
            return;
        }

        /* internal  */gotoCategory(arg1: int): void
        {
            this.updateCategory(arg1);
            this._module.setDoingWinSelectedIndex(arg1);
            return;
        }

        /* internal  */updateCategory(arg1: int): void
        {
            this._module.updateCurWin(this.getCategoryDatas(arg1));
            return;
        }

        /* internal  */getCategoryBonuseHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as AchievementMajorData;
            GameProxy.achievment.getAchivevementCategoryBonus(loc1.categoryId, loc1.index);
            return;
        }

        /* internal  */getAchievementBonuseHandler(arg1: DataEvent): void
        {
            var loc1=int(arg1.data);
            GameProxy.achievment.getAchievementBonus(loc1);
            return;
        }

        /* internal  */getTotalLevelBonuseHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as AchievementMinorData;
            return;
        }

        /* internal  */onBonusGotHandler(arg1: Object): void
        {
            this.updateCanGetBonusEffects();
            return;
        }

        /* internal  */showMinorAchievementThatGotHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as AchievementMinorData;
            var loc2=this.dataParser.parseForFinishedDatas(loc1);
            this.finishedHistoryWin.updateList(loc2);
            this.finishedHistoryWin.show();
            return;
        }

        /* internal  */changeWinHandler(arg1: DataEvent): void
        {
            if (this._module.curWinIndex != AchievementModule.Index_DoingWin) 
            {
                if (this._module.curWinIndex == AchievementModule.Index_ImportantWin) 
                {
                    this.updateImportantWinWhenAddToStage();
                }
            }
            else 
            {
                this.updateDoingWinWhenAddToStage();
            }
            return;
        }

        /* internal  */get finishedHistoryWin(): AchievementFinishedHistoryWin
        {
            if (this._finishedHistoryWin == null) 
            {
                this._finishedHistoryWin = new AchievementFinishedHistoryWin();
            }
            return this._finishedHistoryWin;
        }

        /* internal  */categoryUpdateHandler(arg1: Object): void
        {
            var loc1=0;
            if (this._module.curWinIndex == AchievementModule.Index_DoingWin) 
            {
                loc1 = this._module.getCurTabIndex();
                this.updateCategory(loc1);
            }
            if (this._module.curWinIndex == AchievementModule.Index_ImportantWin) 
            {
                loc1 = EAcieveCategory._EAchieveCategoryImportant;
                this.updateCategory(loc1);
            }
            this.checkAndResetCanGetBonusCodes(loc1);
            this.updateCanGetBonusEffects();
            return;
        }

        /* internal  */overViewUpdateHandler(arg1: Object): void
        {
            this.updateCategory(0);
            this.checkAndResetCanGetBonusCodes(0);
            this.updateCanGetBonusEffects();
            return;
        }

        /* internal  */checkAndResetCanGetBonusCodes(arg1: int): void
        {
            var loc3=null;
            var loc1=cache.achievement.getCanGetBonusIndexs();
            if (!(loc1 == null) && loc1.indexOf(arg1) >= 0) 
            {
                return;
            }
            var loc2=AchievementConfig.instance.getAchievementDetails();
            if (loc2 == null) 
            {
                return;
            }
            var loc4=0;
            var loc5=loc2;
            for(loc3 of loc5) 
            {
                if (loc3.category != arg1) 
                {
                    continue;
                }
                cache.achievement.delCanGetBonuseCode(loc3.code);
            }
            return;
        }

        /* internal  */achievementFinishedHandler(arg1: Object): void
        {
            var data: Object;
            var onGetResFile: Function;

            var loc1;
            onGetResFile = null;
            data = arg1;
            onGetResFile = (): void
            {
                isResFileGot = true;
                resFileGotedAfterFinishedCommand(data);
                return;
            }
            if (this.isResFileGot) 
            {
                this.resFileGotedAfterFinishedCommand(data);
            }
            else 
            {
                LoaderHelp.addResCallBack(ResFileConst.Achievement, onGetResFile);
            }
            return;
        }

        /* internal  */resFileGotedAfterFinishedCommand(arg1: Object): void
        {
            var data: Object;
            var codes: Array<any>;
            var code: int;
            var detail: TAchievementDetatil;
            var nextFrame: Function;
            var type: TAchievementType;

            var loc1;
            nextFrame = null;
            type = null;
            data = arg1;
            nextFrame = (): void
            {
                updateCanGetBonusEffects();
                return;
            }
            codes = data as Array<any>;
            this._finishedCodes = this._finishedCodes.concat(codes);
            if (this._finishedCodes == null || this._finishedCodes.length == 0) 
            {
                return;
            }
            code = this._finishedCodes.shift();
            if (code <= 0) 
            {
                this.resFileGotedAfterFinishedCommand([]);
                return;
            }
            detail = AchievementConfig.instance.getAchievementDetailByCode(code);
            if (detail == null) 
            {
                this.resFileGotedAfterFinishedCommand([]);
                return;
            }
            cache.achievement.addCanGetBonuseCode(code);
            if (detail.category != EAcieveCategory._EAchieveCategoryImportant) 
            {
                cache.achievement.addRecentCode(code);
                if (this._module != null) 
                {
                    this._module.updateLatestList(cache.achievement.recentcodes);
                }
            }
            CallLater.addCallBack(nextFrame);
            if (!detail.needBomdBox) 
            {
                type = AchievementConfig.instance.getAchievementType(detail.category, detail.type);
                if (type == null) 
                {
                    this.resFileGotedAfterFinishedCommand([]);
                    return;
                }
                MsgManager.showRollTipsMsg(Language.getStringByParam(80066, type.name, detail.idx.toString()));
                this.resFileGotedAfterFinishedCommand([]);
                return;
            }
            if (!(this._module == null) && !this._module.isHide) 
            {
                this.resFileGotedAfterFinishedCommand([]);
                return;
            }
            if (this._finishedWin == null) 
            {
                this._finishedWin = new AchievementFinishedWin();
            }
            this._finishedWin.clearTimer();
            this._finishedWin.updateView(detail);
            this._finishedWin.show(6, this.resFileGotedAfterFinishedCommand);
            return;
        }

        private /* var */_module: AchievementModule;

        private /* var */_finishedWin: AchievementFinishedWin;

        private /* var */_icon: AchievementIconBtn;

        private /* var */_finishedCodes: Array<any>;

        private /* var */_finishedHistoryWin: AchievementFinishedHistoryWin;

        private /* var */_isResFileGot: Boolean=false;

        private /* var */_isFirstAddToStage: Boolean=true;

        private /* var */_dataParser: AchievementDataProviderParser;
    }
